package com.sirap.fish.fetchers;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sirap.basic.domain.MexObject;
import com.sirap.basic.tool.D;
import com.sirap.basic.util.Amaps;
import com.sirap.basic.util.Colls;
import com.sirap.basic.util.DateUtil;
import com.sirap.basic.util.EmptyUtil;
import com.sirap.basic.util.FileUtil;
import com.sirap.basic.util.MathUtil;
import com.sirap.basic.util.StrUtil;

public class LawImageFetchers {
	public static void main(String[] args) {
		Object va;
		String ap = "C:/mastorage/albums";
		String ca = "中";
		va = getAlbumsByPage(1, 20, ap, ca);
//		va = getAlbums(3);
//		va = countOfImages("http://sports.163.com/photoview/4TM10005/164442.html#p=E296JT1A4TM10005NOS");
//		D.pl(countOfAlbum("https://www.fenun.cn/Item/Details/20912"));
//		va = getAlbumsByPage(1);
//		va = getAlbumsByPage(82);
//		va = getAlbum("http://slide.sports.sina.com.cn/n_zjmaster/slide_2_789_199926.html#p=5");
//		va = getAlbum("http://news.163.com/photoview/00AO0001/2298204.html?from=ph_ss#p=E24AQNA300AP0001NOS");
		//http://sports.163.com/photoview/4TM10005/164442.html
//		va = getAlbum("http://sports.163.com/photoview/4TM10005/164442.html");
		//http://news.163.com/photoview/00AP0001/2298248.html
//		va = getAlbum("http://news.163.com/photoview/00AP0001/2298249.html#p=E292NB8U00AP0001NOS");
		D.pjsp(va);
	}
	
	public static Map<String, Object> getAlbumsByPage(int page, int pagesize, String albumsPath, String criteria) {
		Map<String, Object> item = Maps.newConcurrentMap();
    	File[] folders = (new File(albumsPath)).listFiles(new FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				if(pathname.isDirectory()) {
					if(EmptyUtil.isNullOrEmpty(criteria)) {
						return true;
					}
					String mexCriteria = criteria.trim();
					MexObject mo = new MexObject(pathname.getName());
					D.pl(pathname.getName());
					if(mo.isMexMatched(mexCriteria)) {
						return true;
					}
				}
				
				return false;
			}
		});
    	
    	int totalpage = MathUtil.totalPageOf(folders.length, pagesize);
    	item.put("totalpage", totalpage);

		List<File> all = Arrays.asList(folders);
		Collections.sort(all, new Comparator<File>(){

			@Override
			public int compare(File f1, File f2) {
				Integer a1 = valueOf(f1);
				Integer a2 = valueOf(f2);
				if(a1 != a2) {
					return a1.compareTo(a2);
				}
				
				Long v1 = f1.lastModified();
				Long v2 = f2.lastModified();
				int val = v2.compareTo(v1);
				if(val == 0) {
					return f2.getAbsolutePath().compareTo(f1.getAbsolutePath());
				} else {
					return val;
				}
			}
			
			private Integer valueOf(File file) {
				if(StrUtil.equals(file.getName(), "upload")) {
					return -1;
				} else {
					return 0;
				}
			}
		});
		
		List<File> kids = Colls.byPage(all, page, pagesize);
		List<Map<String, Object>> mexItems = Lists.newArrayList();
		
		for(File folder : kids) {
			Map<String, Object> row = Amaps.newLinkHashMap();
			row.put("title", folder.getName());
			List<File> files = FileUtil.getSubFilesByLastModifiedDescend(folder.getAbsolutePath());
			for(File current : files) {
				boolean flag = StrUtil.isIn(FileUtil.extensionOf(current.getName()), FileUtil.EXTENSIONS_IMAGE);
				if(flag) {
					row.put("cover", current.getName());
					break;
				}
			}
			String when = DateUtil.strOf(folder.lastModified(), DateUtil.DATE_TIME);
			row.put("when", when);
			row.put("count", FileUtil.getSubFilesByNameAscend(folder.getAbsolutePath()).size());
			mexItems.add(row);
		}
		
		item.put("photos", mexItems);
		
		return item;
	}
}
